//
//  UIView+Extension.m
//  分类
//
//  Created by 裘明 on 15/12/14.
//  Copyright © 2015年 裘明. All rights reserved.
//

#import "UIView+Extension.h"
#import <objc/runtime.h>
#import <QuartzCore/QuartzCore.h>

@implementation UIView (Extension)

@dynamic info_Dict;

static const void *IndieBandNameKey = &IndieBandNameKey;

#pragma mark - 设置Frame
#pragma mark 设置view的X值
- (void)setX:(CGFloat)x
{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}
- (CGFloat)x
{
    return self.frame.origin.x;
}

#pragma mark 设置view的y值
- (void)setY:(CGFloat)y
{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}
- (CGFloat)y
{
    return self.frame.origin.y;
}

#pragma mark 设置view的宽度
- (void)setWidth:(CGFloat)width
{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}
- (CGFloat)width
{
    return self.frame.size.width;
}

#pragma mark 设置view的高度
- (void)setHeight:(CGFloat)height
{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}
- (CGFloat)height
{
    return self.frame.size.height;
}

#pragma mark 设置view的Origin
- (void) setOrigin:(CGPoint)point
{
    self.frame = CGRectMake(point.x, point.y, self.frame.size.width, self.frame.size.height);
}
- (CGPoint) origin
{
    return self.frame.origin;
}

#pragma mark 设置view的Size
- (void)setSize:(CGSize)size
{
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}
- (CGSize)size
{
    return self.frame.size;
}


#pragma mark 设置view的bottom
- (void)setBottom:(CGFloat)bottom
{
    self.frame = CGRectMake(self.x, bottom - self.height, self.width, self.height);
}
- (CGFloat)bottom
{
    return self.y + self.height;
}

#pragma mark 设置view的right
- (void)setRight:(CGFloat)right
{
    self.frame = CGRectMake(right - self.width, self.y, self.width, self.height);
}
- (CGFloat)right
{
    return self.x + self.width;
}

// 设置Y坐标，距离aboveView的interval距离
- (void) setYAbove:(float)interval withView:(UIView *)aboveView{
    self.y = aboveView.y + aboveView.height+interval;
}
// 在父控件中左右居中
- (void) autoWCenter{
    self.x = self.superview.width/2 - self.width/2;
}
// 在父控件中上下居中
- (void) autoHCenter{
    self.y = self.superview.height/2 - self.height/2;
}

#pragma mark - 切换效果
- (void)switchContentViewWithAnimation:(NSString *)direction {
    [self switchContentViewWithAnimation:direction type:kCATransitionPush duration:0.3f];
}
- (void)switchContentViewWithAnimation:(NSString *)direction type:(NSString *)transitionType {
    [self switchContentViewWithAnimation:direction type:transitionType duration:0.3f];
}
- (void)switchContentViewWithAnimation:(NSString *)direction type:(NSString *)transitionType duration:(float)duration {
    CATransition *animation = [CATransition animation];
    // 动画时间
    animation.duration = duration;
    // 先慢后快
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
    animation.fillMode = direction;
    animation.removedOnCompletion = NO;
    // 各种组合
    animation.type = transitionType;
    animation.subtype = direction;
    
    [self.layer addAnimation:animation forKey:@"animation"];
}

#pragma mark - view加载
// view转换成图片
- (UIImage*)getConvertViewToImage{
    UIGraphicsBeginImageContext(self.bounds.size);
    
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    
    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return image;
}
// 根据Xib文件加载View
+ (instancetype)createWithXibName:(NSString *)xibName
{
    if (!xibName) {
        id temp = [[[self class] alloc]init];
        return temp;
    } else {
        NSArray* nibView =  [[NSBundle mainBundle] loadNibNamed:xibName owner:nil options:nil];
        return [nibView objectAtIndex:0];
    }
}

// 把所有的子View加在父View里
- (void)addSubVs:(UIView *)object, ...
{
    va_list argp;
    va_start(argp, object);
    UIView *tempObject = nil;
    tempObject = object;
    while(tempObject != nil){
        [self addSubview:tempObject];
        tempObject = va_arg(argp, UIView *);
    }
    va_end(argp);
}

#pragma mark - view转换成Image
// 给view添加按键事件
-(void) addTagEven:(SEL) mSel withTarget:(id)target{
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:target action:mSel];
    singleTap.delegate = (id<UIGestureRecognizerDelegate>)self;
    [self addGestureRecognizer:singleTap];
}

// 给view添加按键事件
static char addTagEvenBlockKey;
// 添加view的tag事件到block上
- (void)addTagEvenBlock:(void (^)(UIView *sender))block
{
    objc_setAssociatedObject(self, &addTagEvenBlockKey, block, OBJC_ASSOCIATION_COPY);
    [self addTagEven:@selector(onClickTagBlock:) withTarget:self];
}

// 添加view的tag事件到block 长按事件
- (void)addTagLongEvenBlock:(void (^)(UIView *sender))block
{
    objc_setAssociatedObject(self, &addTagEvenBlockKey, block, OBJC_ASSOCIATION_COPY);
    [self addTagEven:@selector(onLongTapTagBlock:) withTarget:self];
}

#pragma mark - UITapGesture delegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([touch.view isKindOfClass:[UIButton class]]) {
        return NO;
    }
    return YES;
}

// 点击view的除了textview和textfiled的地方就隐藏
- (void)hideKeyboardWithOutTapTextFiledAndTextView:(NSArray *)views
{
    [self addTagEvenBlock:^(UIView *sender) {
        if (![sender isKindOfClass:[UITextField class]] && ![sender isKindOfClass:[UITextView class]]) {
            for (UIView *eachView in views) {
                if ([eachView isKindOfClass:[UITextView class]]) {
                    [((UITextView *)eachView) resignFirstResponder];
                }else if ([eachView isKindOfClass:[UITextField class]]) {
                    [((UITextField *)eachView) resignFirstResponder];
                }
            }
        }
    }];
}

- (void)onClickTagBlock:(UITapGestureRecognizer *)singleTap
{
    void (^block)(UIView *sender)  = objc_getAssociatedObject(self,&addTagEvenBlockKey);
    if (block) {
        block(singleTap.view);
    }
}

- (void)onLongTapTagBlock:(UILongPressGestureRecognizer *)longTap
{
    longTap.minimumPressDuration = 0.7; //seconds  设置响应时间
    void (^block)(UIView *sender)  = objc_getAssociatedObject(self,&addTagEvenBlockKey);
    if (block) {
        block(longTap.view);
    }
}

#pragma mark 详细属性设置
- (void)setInfo_Dict:(NSDictionary *)info_Dict
{
    objc_setAssociatedObject(self, IndieBandNameKey, info_Dict, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSDictionary *)info_Dict
{
    return objc_getAssociatedObject(self, IndieBandNameKey);
}

// 清理背景色
- (void) clearBackground{
    self.backgroundColor = [UIColor clearColor];
}
@end

// MARK - IBExtension
@implementation UIView (IBExtension)

- (void)setBorderColor:(UIColor *)borderColor {
    self.layer.borderColor = borderColor.CGColor;
}

- (UIColor *)borderColor {
    return [UIColor colorWithCGColor:self.layer.borderColor];
}

- (void)setBorderWidth:(CGFloat)borderWidth {
    self.layer.borderWidth = borderWidth;
}

- (CGFloat)borderWidth {
    return self.layer.borderWidth;
}

- (void)setCornerRadius:(CGFloat)cornerRadius {
    self.layer.cornerRadius = cornerRadius;
    self.layer.masksToBounds = YES;
    
    // 栅格化 - 提高性能
    // 设置栅格化后，图层会被渲染成图片，并且缓存，再次使用时，不会重新渲染
    self.layer.rasterizationScale = [UIScreen mainScreen].scale;
    self.layer.shouldRasterize = YES;
}

- (CGFloat)cornerRadius {
    return self.layer.cornerRadius;
}

@end







